package com.example.cloudproduct.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 2 * @Author: ZhangShuai
 * 3 * @Date: 2020/6/24 10:19
 * 4 拦截request请求 每次去获得请求头
 */
@Component
public class AuthorizationInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String token = request.getHeader("token");
        System.out.println("token = " + token);
        String authorization = request.getHeader("Authorization");
        System.out.println("authorization = " + authorization);
        requestTemplate.header("token", token);
        requestTemplate.header("Authorization", authorization);
    }

}
